<?php namespace Home\Controller; 
use Think\Controller;

/**
 * Class 注册控制器
 * @package Home\Controller
 */
class RegController extends CommonController
{
	public function index(){

        $question =D('Verify')->getOneVerify();

        $this->assign('question',$question);

        $this->display();

	}

/**
 * [checkdate description]
 * @return  数据检测
 */
	public function CheckDate()
	{
		//POST 获取两次密码
		$password=$_POST['password'];
		$repassword=$_POST['repassword'];
		
		//判断两次密码是否相同
		if ($password!=$repassword) {
			$arr = array(
    			'error'=>1,
    			'info'=>'两次密码不一致'
    			);
    		echo json_encode($arr);
    		exit;
		}
		//POST 	获取人类验证
		$answer = $_POST['answer'];
		//判断人类验证
		if ($answer!=$_SESSION['answer']) {
			$arr = array(
    			'error'=>1,
    			'info'=>'人类验证失败，请重新输入！'
    			);
    		echo json_encode($arr);
    		exit;
		}
		//POST 	获取邮箱
		$email = $_POST['email'];
		//检测邮箱是否存在
		$sql="select email from user where email='{$email}'";
		$res=M()->query($sql);
		if ($res) {
			$arr = array(
    			'error'=>1,
    			'info'=>'邮箱己存在！'
    			);
    		echo json_encode($arr);
    		exit;
		}

		//POST 	获取昵称
		$nickname = $_POST['nickname'];
		//检测昵称是否存在
		$sql="select nickname from user where nickname='{$nickname}'";
		$res=M()->query($sql);
		if ($res) {
			$arr = array(
    			'error'=>1,
    			'info'=>'昵称己存在，请重新输入！'
    			);
    		echo json_encode($arr);
    		exit;
		}

		//获取POST表单所有信息
		$data=$_POST;
		//账号全部转小写
		$data['email'] =strtolower($data['email']);
		//密码MD5加密
		$data['password']=md5($data['password']);
		//增加时间
		$data['create_time']=time();

		// $ip='61.140.122.246';

		$data['city']=$this->getcity(get_client_ip());

		//增加用户
		if (M('user')->add($data)) {
			$_SESSION['answer'] ='';
			$arr = array(
    			'error'=>0,
    			'info'=>'注册成功！'
    			);
    		echo json_encode($arr);
    		exit;
		}else{
			$arr = array(
    			'error'=>1,
    			'info'=>'注册失败'
    			);
    		echo json_encode($arr);
    		exit;
		}


	}

	//ip接口
	public function getcity($ip)
	{
		$url='http://ip.taobao.com/service/getIpInfo.php?ip='.$ip;

		$data= file_get_contents($url);

		$arr= json_decode($data, true);

		if ($arr['code']==0) {
			return $arr['data']['city'];
		}
	}

	public function refres(){
		if ($question =D('Verify')->getOneVerify()) {
			echo json_encode(array('error'=>0,'info'=>$question));
			exit();
		}else{
			echo json_encode(array('error'=>1,'info'=>'网络忙，请重试！'));
			exit();
		}
		
	}




}
